$(document).ready(function () {
	function showError(str,delay){	
		  if(delay){
			  $('#alertMessage').removeClass('success info warning').addClass('error').html(str).stop(true,true).show().animate({ opacity: 1,right: '10'}, 500,function(){
					  $(this).delay(delay).animate({ opacity: 0,right: '-20'}, 500,function(){ $(this).hide(); });																														   																											
				});
			  return false;
		  }
			  	$('#alertMessage').addClass('error').html(str).stop(true,true).show().animate({ opacity: 1,right: '10'}, 500);	
	  }
	  function showSuccess(str,delay){
		  if(delay){
			  $('#alertMessage').removeClass('error info warning').addClass('success').html(str).stop(true,true).show().animate({ opacity: 1,right: '10'}, 500,function(){
					  $(this).delay(delay).animate({ opacity: 0,right: '-20'}, 500,function(){ $(this).hide(); });																														   																											
				});
			  return false;
		  }
			  $('#alertMessage').addClass('success').html(str).stop(true,true).show().animate({ opacity: 1,right: '10'}, 500);	
	  }
	  
	var list_id = '';		
	$(".choose_product").live("change", function(){
		$('#search_product').validationEngine('hide');
		var id = $(this).val().substr(0,$(this).val().length - 8);
		n = list_id.indexOf(id);
		if (n == -1) {
			var dataString = "id=" + id ;
			$("#result_choose_product").html("<img src='../img/loadder/loader_green.gif'/>");
			$.ajax({
		        type: "GET",
		        url: "choose_product_retail",
		        data: dataString,
		        dataType: 'html',
		        success: function (data) {
					list_id = list_id + id + ',';
		            $("#result_choose_product").html(data).hide().fadeIn();
		            $("#result_choose_product").attr('id',id+'_product');
		            $("#list_product").append("<div id='result_choose_product'></div>");		             
		            $('.numericonly input').autotab_magic().autotab_filter('numeric');
		            $('form#validation').validationEngine();
		        }
		    })
		}
		else
		{
			showError("Đã thêm sản phẩm này",1000)
		}
		
	})
	
	
	$(".remove_product").live("click", function(){
		var id = $(this).attr('id').substr(0,$(this).attr('id').length-7);
		list_id = list_id.replace(id + ',','');
		showSuccess('Đã xóa sản phẩm',1000);
		$("#" + id + "_product").remove();
	})
	
	
	$("#create_retail_invoice").live("click", function(){
		var list_quantity = '';
		if (list_id == '')
		{
			$('#search_product').validationEngine('showPrompt', 'Chọn sản phẩm', 'pass','topLeft');
			showError('Chọn sản phẩm hoặc thêm mới sản phẩm!',1000);
		}
		else
		{
			$('#search_product').validationEngine('hide');
			if($("#list_product").validationEngine('validate'))
			{
				var list_id_temp = list_id.substr(0,list_id.length-1);
				var cus_name = $('#cus_name').val();
				var cus_phone = $('#cus_phone').val();
				var cus_address = $('#cus_address').val();
				var array_id = list_id_temp.split(',');
				for (var i=0; i < array_id.length; i++)
				{
					id = array_id[i];
					list_quantity = list_quantity + $("#" + id + '_quantity').val() + ',';
				}
				var dataString = "list_id=" + list_id + "&cus_name=" + cus_name + "&cus_phone=" + cus_phone + "&cus_address=" + cus_address + "&list_quantity=" + list_quantity 
				$.ajax({
			        type: "GET",
			        url: "create_retail_invoice",
			        data: dataString,
			        dataType: 'html',
			        success: function (data) {
			        	showSuccess('Tạo hóa đơn thành công',1000);
			        }
			    })
			}
			else
			{
				$("#list_product").validationEngine('validate')
			}
		}
	})
	
	
	$(".onChange").live("keyup",function() {
		var id = "" 
		id = $(this).attr('id').substr(0,$(this).attr('id').length-9); 
		if ($("#" + id + "_quantity") != "")
		{
			var sum = ""
			sum = $("#" + id + "_quantity").val() * $("#" + id + "_price").text(); 
			$("#" + id + "_sum").val(sum)
		}
		var list_id_temp = list_id.substr(0,list_id.length-1);
		var array_id = list_id_temp.split(',');
		var total = parseInt(0,10);
		for (var i=0; i < array_id.length; i++)
		{
			id = array_id[i];
			total += parseInt($("#" + id + "_sum").val())
		}
		$("#sum_result").html(total)
	})
	
	
})